public class IDUtil {


    static int getWorkerId(int serverid, IdType IdType) {
        assert serverid <= SnowflakeIdWorker.maxWorkerId && IdType.value <= SnowflakeIdWorker.maxDatacenterId && serverid >= 0 && IdType.value >= 0;
        return serverid;
    }

    static int getDataCenterId(int serverid, IdType IdType) {
        assert serverid <= SnowflakeIdWorker.maxWorkerId && IdType.value <= SnowflakeIdWorker.maxDatacenterId && serverid >= 0 && IdType.value >= 0;
        return IdType.value;
    }

    /**
     *
     * @param serverid worker
     * @param IdType datacenterId
     * @return
     */
    public static SnowflakeIdWorker genIdWorker(int serverid, IdType IdType) {
        int workerId = getWorkerId(serverid, IdType);
        int dataCenterId = getDataCenterId(workerId, IdType);
        return new SnowflakeIdWorker(workerId, dataCenterId);
    }

    public static int compare(long id1, long id2) {
        if (id1 == id2) return 0;
        long t1 = SnowflakeIdWorker.getTime(id1);
        long t2 = SnowflakeIdWorker.getTime(id2);
        if (t1 > t2) return 1;
        else if (t1 < t2) return -1;
        else {
            int seq1 = SnowflakeIdWorker.getSeq(id1);
            int seq2 = SnowflakeIdWorker.getSeq(id2);
            return seq1 > seq2 ? 1 : (seq1 < seq2 ? -1 : 0);
        }
    }

    public static int getServerId(long id) {
        int workerId = SnowflakeIdWorker.getWorker(id);
        return workerId;
    }

    public static IdType getType(long id) {
        int dataCenterId = SnowflakeIdWorker.getDataCenter(id);
        return IdType.fromNum(dataCenterId);
    }

    public static void main(String[] args) {

        System.out.println(SnowflakeIdWorker.maxDatacenterId);
        System.out.println(SnowflakeIdWorker.maxWorkerId);

        System.out.println(".............");

        test(1, IdType.None);
        test(Long.valueOf(SnowflakeIdWorker.maxWorkerId).intValue(), IdType.MAX);

    }

    public static void test(int serverId, IdType idType) {

        SnowflakeIdWorker s11 = genIdWorker(serverId, idType);
        long l = s11.nextId();

        System.out.println(l);
        System.out.println(getServerId(l));
        System.out.println(getType(l));
        System.out.println(".............");
    }

}